package org.aperture.eve.data;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

public class Parse_Skill_Sheet {
	
	public static void main(String[] args) {
		File file = new File("D://javatest//Sabrah_Skills.xml");
		parseSkillSheet(getSkillSheet(file));
	}

	private static void parseSkillSheet(Document skillSheet) {
//		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
////		builderFactory.setValidating(true);
////		builderFactory.setIgnoringElementContentWhitespace(true);
//		DocumentBuilder builder = null;
//		try {
//			builder = builderFactory.newDocumentBuilder();
//		} catch (ParserConfigurationException e) {
//			e.getStackTrace();
//		}
//		
//		try {
//			Document document = builder.parse(file);
//			Element rootElement = document.getDocumentElement();
//			
//			removeWhitespaceNodes(rootElement);
//			
//			Node characterRootElement = null;
////			System.out.println("Character ID: " + rootElement.getTextContent());
//			
//			NodeList nodes = rootElement.getChildNodes();

//			for (int i = 0; i <nodes.getLength(); i++) {
//				Node node = nodes.item(i);
//				if (node.getNodeType()==Node.TEXT_NODE) {
//					node.getParentNode().removeChild(node);
//				}
//			}
			
			
			
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				
				
//				int count_elem=0;  
//				//given the "someNode"  
//				NodeList nlist=someNode.getChildNodes();  
//				for (int i=nlist.getLength()-1; i>-1; i--) {  
//				    Node node=nlist.item(i);  
//				    if (node.getNodeType()==Node.ELEMENT_NODE) {  
//				        count_elem++;  
//				    }  
//				    if (node.getNodeType()==Node.TEXT_NODE) {  
//				        //if you are not 100% it contains only ignorable whitespace, add some testing here  
//				        node.getParentNode().removeChild(node);  
//				    }  
//				}  
				
				
				
				
				
				
				
				if (node instanceof Element) {
					
					String nodeName = node.getNodeName();
					System.out.println("Node name( " + i + "): " + nodeName);
					if (!nodeName.equals("#text")) {
//						System.out.println(i + ": " + node.getNodeName());
					}
					
					if (nodeName.equals("result")) {
						characterRootElement = node;
					}
					
				}
			}
			
			nodes = characterRootElement.getChildNodes();
			
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				
				if (node instanceof Element) {
					String nodeName = node.getNodeName();
					Element child = (Element)node;
					switch (nodeName) {
						case "characterID": System.out.println("Character ID: " + child.getTextContent());
							break;
						case "name": System.out.println("Name: " + child.getTextContent());
							break;
						case "DoB": System.out.println("Date of Birth: " + child.getTextContent());
							break;
						case "race": System.out.println("Race: " + child.getTextContent());
							break;
						case "bloodLine": System.out.println("Blood Line: " + child.getTextContent());
							break;
						case "ancestry": System.out.println("Ancestry: " + child.getTextContent());
							break;
						case "gender": System.out.println("Gender: " + child.getTextContent());
							break;
						case "corporationName": System.out.println("Corporation: " + child.getTextContent());
							break;
						case "corporationID": System.out.println("Corporation ID: " + child.getTextContent());
							break;
						case "allianceName": System.out.println("Alliance: " + child.getTextContent());
							break;
						case "cloneName": System.out.println("Clone Type: " + child.getTextContent());
							break;
						case "cloneSkillPoints": System.out.println("Clone Skill Points: " + child.getTextContent());
							break;
						case "balance": System.out.println("Wallet Balance: " + child.getTextContent());
							break;
						case "attributeEnhancers": {
							NodeList implantsList = node.getChildNodes();
//							for (int j = 0; j < implantsList.getLength(); j++) {
//								Node implantNode = implantsList.item(j);
//								if (implantNode.getNodeType() == Node.TEXT_NODE) {
//									implantNode.getParentNode().removeChild(implantNode);
//								}
//							}
							
							for (int j = 0; j < implantsList.getLength(); j++) {
								Node implantNode = implantsList.item(j);
								System.out.println("Node name: " + implantNode.getNodeName());
								System.out.println("Child Node name: " + implantNode.getFirstChild().getNodeName());
								System.out.println("Next Child Node: " + implantNode.getFirstChild().getNextSibling().getNodeName());
							}
							
							
							
							
							
							
							
							
//							for (int j = 0; j < implantsList.getLength(); j++) {
//								Node implantNode = implantsList.item(j);
								
								
								
								
//								if (!implantNode.getNodeName().equals("#text")) {
//									if (implantNode.getNodeType() == Node.ELEMENT_NODE) {
//										Element implantElement = (Element)implantNode;
//										System.out.println("Implant: " + implantElement.getTagName());
//										System.out.println("Test: " + implantElement.getFirstChild().getNodeValue());
//									}
//								}
								
//								System.out.println("Node type: " + implantNode.getNodeName());
//								if (implantNode instanceof Element) {
//									Element implantElement = (Element)implantNode;
									
//									System.out.println("Node: " + implantsList.item(j).getFirstChild().getTextContent());
//									System.out.println("Node: " + implantsList.item(j).getNodeName());
//									System.out.println("Node's child: " + implantsList.item(j).getFirstChild().getNextSibling().getNodeName());
//									System.out.println("Implant: " + implantsList.item(j).getFirstChild().getNextSibling().getTextContent());
//									System.out.println("Node: " + implantElement.getTagName());
//									System.out.println("Implant Bonus: " + implantsList.item(j).getFirstChild().getNextSibling().getNextSibling().getNextSibling().getTextContent());
								}
								
//							}
//							if (!nodeName.equals("#text")) {
//								System.out.println("Memory Augmentation: " + implants..getTextContent());	
//							}
							
//							System.out.println("Node: " + implants.getFirstChild().getNodeName());
//							System.out.println("Memory Augmentation: " + implants.getFirstChild().getTextContent());
//							implants.getNextSibling();
//							System.out.println("Willpower Augmentation: " + implants.getFirstChild().getTextContent());
//						}
						
					
						default:
					}
					
//					if (nodeName == "characterID") {
////						Element child = (Element)node;
//						System.out.println("Character ID: " + child.getTextContent());
					}
				}
//			}
			
//		} catch (SAXException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		
	}
	
	
//	Removes whitespace text only elements
	
	public static Document getSkillSheet(File file) {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		
		try {
			builder = builderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.getStackTrace();
		}
		
		Document document = null;
		
		try {
			document = builder.parse(file);
			Element rootElement = document.getDocumentElement();
			
			removeWhitespaceNodes(rootElement);
			
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return document;
	}
//	
//	34:    private static Document getDocument(String name)
//	  35:	      {
//	  36:        try
//	  37:	          {
//	  38:            DocumentBuilderFactory factory =
//	  39:                DocumentBuilderFactory.newInstance();
//	  40:            factory.setIgnoringComments(true);
//	  41:            factory.setIgnoringElementContentWhitespace(true);
//	  42:            factory.setValidating(true);
//	  43:            DocumentBuilder builder =
//	  44:                factory.newDocumentBuilder();
//	  45:            return builder.parse(new InputSource(name));
//	  46:        }
//	  47:        catch (Exception e)
//	  48:	          {
//	  49:            System.out.println(e.getMessage());
//	  50:        }
//	  51:        return null;
//	  52:    }
	
	public static void removeWhitespaceNodes(Element e) {
		NodeList children = e.getChildNodes();
		for (int i = children.getLength() - 1; i >= 0; i--) {
			Node child = children.item(i);
			if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
				e.removeChild(child);
			}
			else if (child instanceof Element) {
				removeWhitespaceNodes((Element) child);
			}
		}
	}
	
//	Original function
//	
//	public static void removeWhitespaceNodes(Element e) {
//		NodeList children = e.getChildNodes();
//		for (int i = children.getLength() - 1; i >= 0; i--) {
//			Node child = children.item(i);
//			if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
//				e.removeChild(child);
//			}
//			else if (child instanceof Element) {
//				removeWhitespaceNodes((Element) child);
//			}
//		}
//	}
	
}
